package cn.chen.service.impl;

import cn.chen.dao.mapper.CommentMapper;
import cn.chen.dao.pojo.Comment;
import cn.chen.dao.pojo.SysUser;
import cn.chen.service.CommentsService;
import cn.chen.service.SysUserService;
import cn.chen.utils.UserThreadLocal;
import cn.chen.vo.CommentVo;
import cn.chen.vo.Result;
import cn.chen.vo.UserVo;
import cn.chen.vo.params.CommentParam;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
@Service
public class CommentsServiceImpl implements CommentsService {

    @Autowired
    private CommentMapper commentMapper;
    @Autowired
    private SysUserService sysUserService;

    //二次封装
    private List<CommentVo> copyList(List<Comment> comments) {
        List<CommentVo> commentVoList=new ArrayList<>();
        for (Comment comment : comments) {
            commentVoList.add(copy(comment));
        }
        return commentVoList;
    }

    private CommentVo copy(Comment comment) {
        CommentVo commentVo=new CommentVo();
        BeanUtils.copyProperties(comment,commentVo);
        //作者信息
        Long articleId = comment.getArticleId();
        UserVo userVo = sysUserService.findUserVoById(articleId);
        commentVo.setAuthor(userVo);
        //子评论
        Integer level = comment.getLevel();
        if(level==1){
            Long id = comment.getId();//作为父id
            List<CommentVo> commentVoList=findCommentByParentId(id);
            commentVo.setChildrens(commentVoList);
        }
        //to User 给谁评论
        if(level>1){
            Long toUid = comment.getToUid();
            UserVo toUserVo =sysUserService.findUserVoById(toUid);
            commentVo.setToUser(toUserVo);
        }
        return commentVo;
    }
    /**
     * 评论列表
     * @param articleId
     * @return
     */
    @Override
    public Result commentsByArticleId(Long articleId) {

        /**
         * 1 根据文章id 查询 评论列表 从comment表中查询
         * 2 根据作者的id查询作者信息 从user表查
         * 3 判断如果level=1,要去查询它有没有子评论
         * 4 如果有 根据评论id 进行查询(parent_id)
         */
        LambdaQueryWrapper<Comment> queryWrapper=new LambdaQueryWrapper();
        queryWrapper.eq(Comment::getArticleId,articleId);
        queryWrapper.eq(Comment::getLevel,1);
        List<Comment> comments = commentMapper.selectList(queryWrapper);

        List<CommentVo> commentVoList=copyList(comments);
        return Result.success(commentVoList);
    }

    private List<CommentVo> findCommentByParentId(Long id) {
        LambdaQueryWrapper<Comment> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(Comment::getParentId,id);
        queryWrapper.eq(Comment::getLevel,2);
        return copyList(commentMapper.selectList(queryWrapper));
    }

    /**
     * 评论
     * @param commentParam
     * @return
     */
    @Override
    public Result comment(CommentParam commentParam) {
        SysUser sysUser = UserThreadLocal.get();
        Comment comment = new Comment();
        comment.setArticleId(commentParam.getArticleId());
        comment.setContent(commentParam.getContent());
        comment.setAuthorId(sysUser.getId());
        comment.setCreateDate(System.currentTimeMillis());

        Long parent = commentParam.getParent();
        if(parent==null || parent ==0){
            comment.setLevel(1);
        }else{
            comment.setLevel(2);
        }
        comment.setParentId(parent==null?0:parent); //父评论id
        Long toUserId = commentParam.getToUserId();
        comment.setToUid(toUserId==null?0:toUserId);//被评论的用户id
        this.commentMapper.insert(comment);

        return Result.success(null);
    }
}
